﻿using Main.DataBaseContext;
using Main.dto;
using Main.messageEvent;
using Microsoft.EntityFrameworkCore;
using Prism.Events;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Main.Views
{
    /// <summary>
    /// Interaction logic for IndexView
    /// </summary>
    public partial class IndexView : UserControl
    {
        private readonly BusnessContext busnessContext;

        public CollectionViewSource ToDoTaskViewSource { get; }


        public IndexView(BusnessContext busnessContext,IEventAggregator eventAggregator)
        {
            InitializeComponent();

            this.busnessContext = busnessContext;

            eventAggregator.GetEvent<RefreshMainWindowEvent>().Subscribe(Refresh);

            ToDoTaskViewSource = (CollectionViewSource)FindResource(nameof(ToDoTaskViewSource));

        }

       


        private void Refresh(string param)
        {

            ToDoTaskGrid.Items.Refresh();

            MemosGrid.Items.Refresh();
        }


        private async void ToDoTaskGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {

            if (e.EditAction == DataGridEditAction.Commit)
            {

                ToDoTask toDoTask = (ToDoTask)e.Row.Item;

                Action<string> updateFunc = val => {


                };

                if (e.Column is DataGridTextColumn column)
                {
                    switch (((Binding)column.Binding).Path.Path)
                    {

                        case "Detail":
                            {
                                updateFunc = val =>
                                {
                                    toDoTask.Detail = val;
                                };
                                break;
                            };
                        case "TaskSummary":
                            {
                                updateFunc = val =>
                                {
                                    toDoTask.TaskSummary = val;
                                };
                                break;
                            }
                        case "Status":
                            {
                                updateFunc = val =>
                                {
                                    toDoTask.Status = int.Parse(val);
                                };
                                break;
                            }


                    }
                }

                switch (e.EditingElement)
                {
                    case TextBox textBox:
                        {


                            updateFunc(textBox.Text);
                            break;
                        }
                    default:
                        {
                            MessageBox.Show("不支持的元素编辑类型");
                            break;
                        }
                }


                await busnessContext.SaveChangesAsync();
                //Refresh(""); 编辑过程中不允许刷新
            }

        }

        private void MemosGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            if (e.EditAction == DataGridEditAction.Commit)
            {
            }
        }

        private async void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            await busnessContext.ToDoTasks.LoadAsync();

            ToDoTaskViewSource.Source = busnessContext.ToDoTasks.Local.ToObservableCollection();
        }
    }
}
